package com.jtlrm.ckd.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.DateFormatUtil;
import com.base.sdk.util.ImageUtil;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hyphenate.chatuidemo.domain.NewUserEntity;
import com.hyphenate.chatuidemo.utils.UserUtil;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.UserEntity;
import com.jtlrm.ckd.entity.event.UserInfoMessage;
import com.jtlrm.ckd.mvp.model.UserModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.BindView;

public class PersonInfoActivity extends TitleBarActivity {

    @BindView(R.id.avatar)
    ImageView avatarI;
    @BindView(R.id.name)
    TextView nameT;
    @BindView(R.id.sex)
    TextView sexT;
    @BindView(R.id.birthday)
    TextView birthdayT;
    @BindView(R.id.phone)
    TextView phoneT;
    @BindView(R.id.hospital)
    TextView hospitalT;
    @BindView(R.id.role)
    TextView roleT;
    TimePickerView birthDay;
    UserModel userModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected String getTitleText() {
        return "我的资料";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_person_info);
    }

    @Override
    protected void initView() {
        userModel = new UserModel();
        EventBusUtil.register(this);
        freshInfo();
        birthDay = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                setText(birthdayT, DateFormatUtil.dateFormatAll(date.getTime()));
                uploadBirthday(date);
            }
        }).build();
    }

    private void freshInfo() {
        UserEntity userEntity = UserHelper.getInstance(context).getUserInfo();
        if (userEntity != null) {
            ImageUtil.loadCircleImage(avatarI, userEntity.getHeadPicture(), R.drawable.person_change);
            setText(nameT, userEntity.getName());
            setText(sexT, userEntity.getSex());
            setText(birthdayT, userEntity.getBrithday());
            setText(phoneT, userEntity.getMobile());
            setText(hospitalT, userEntity.getHospitalName());
            setText(roleT, userEntity.getRole());
        }
    }

    private void uploadBirthday(Date date) {
        showLoadingDialog("修改中");
        String birthday = DateFormatUtil.dateFormatLine(date.getTime());
        userModel.changeBirthday(birthday, new CommonObserver<String>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(String data) {
                showToast("出生日期修改成功");
                updateUserInfo();
            }
        }, lifecycleSubject);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }

    public void selectHeader(View view) {
        startActivity(new Intent(context, HeaderChangeActivity.class));
    }

    public void changeBirthDay(View view) {
        if (birthDay != null) {
            birthDay.show();
        }
    }

    public void updateUserInfo() {
        userModel.getUserInfo(new CommonObserver<UserEntity>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(UserEntity data) {
                dismissLoadingDialog();
                UserHelper.getInstance(context).setUserInfo(data);
                EventBusUtil.postEvent(new UserInfoMessage(true));
                NewUserEntity entity = new NewUserEntity();
                entity.setImId(data.getEasemobId());
                entity.setAvatar(data.getHeadPicture());
                entity.setUserId(data.getId());
                entity.setName(data.getName());
                entity.setRole(data.getRole());
                UserUtil.getInstance().saveUser(entity);
            }
        }, lifecycleSubject);
    }

    public void changePhone(View view) {
        startActivity(new Intent(context,   ChangePhoneActivity.class));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UserInfoMessage event) {
        if (event.isUpdateMesaage()) {
            freshInfo();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
